//
// Created by REEJI2219 on 2024-09-18.
//
//通过数组的方式存储对称矩阵
#include <iostream>
using namespace std;

/**
 * 单维数组存储压缩矩阵
 */
class SymmetricMatrix {
private:
    int *array;
    int n;
public:
    SymmetricMatrix(int n) {
        int size = n*(n+1)/2;
        array = new int[size];
        for(int i=0;i<size;i++) {
            array[i]=i;
        }
    }

    /**
     * 通过行、列获取数据
     * @param cal 行
     * @param row 列
     * @return
     */
    int getData(int cal,int row) {
        int k=0;
        if(cal>row) {
            k = cal*(cal-1)/2+row-1;
        }else {
            k = row*(row-1)/2+cal-1;
        }
        return array[k];
    }
};

// int main(int argc, char *argv[]) {
//     SymmetricMatrix *matrix = new SymmetricMatrix(5);
//     cout<<matrix->getData(5,4)<<endl;
//     cout<<matrix->getData(4,5)<<endl;
// }
